package com.yangtzeu.jwc.fragment;

import java.util.LinkedList;
import java.util.List;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.yangtzeu.jwc.activity.AboutActivity;
import com.yangtzeu.jwc.activity.FeedbackActivity;
import com.yangtzeu.jwc.activity.LoginActivity;
import com.yangtzeu.jwc.activity.MainActivity;
import com.yangtzeu.jwc.activity.NewsActivity;
import com.yangtzeu.jwc.activity.NoteActivity;
import com.yangtzeu.jwc.activity.XkActivity;
import com.yangtzeu.jwc.businessservice.impl.StudentServiceImpl;
import com.yangtzeu.jwc.customview.CustomeProgressDialog;
import com.yangtzeu.jwc.customview.cooldraganddrop.CoolDragAndDropGridView;
import com.yangtzeu.jwc.customview.cooldraganddrop.Item;
import com.yangtzeu.jwc.customview.cooldraganddrop.ItemAdapter;
import com.yangtzeu.jwc.customview.cooldraganddrop.SimpleScrollingStrategy;
import com.yangtzeu.jwc.customview.cooldraganddrop.SpanVariableGridView;
import com.yangtzeu.jwc.utils.ConfirmDialog;
import com.yangtzeu.jwc.utils.G;
import com.zhangdi.jwc.R;

public class MoreFragment extends Fragment implements OnClickListener, CoolDragAndDropGridView.DragAndDropListener, SpanVariableGridView.OnItemClickListener, SpanVariableGridView.OnItemLongClickListener {
    private Handler myHandler = new MyHandler();
    private ProgressDialog dialog;
    private LinearLayout ll_btn_cet, ll_btn_xuanke, ll_btn_map, ll_btn_tieba, ll_info;
    private ImageButton ib_btn_info, ib_btn_about, ib_btn_logout;

    ItemAdapter mItemAdapter;
    CoolDragAndDropGridView mCoolDragAndDropGridView;
    List<Item> mItems = new LinkedList<Item>();

    // private TextView more_notice_dialog;

    public class MyHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    dialog = CustomeProgressDialog.show(getActivity(), "正在加载数据...");
                    dialog.setCancelable(true);
                    break;
                case 2:
                    if (dialog != null)
                        dialog.dismiss();
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_more, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    /*
     * ll_btn_cet = (LinearLayout)
	 * getActivity().findViewById(R.id.ll_btn_cet); ll_btn_xuanke =
	 * (LinearLayout) getActivity().findViewById(R.id.ll_btn_xuanke);
	 * ll_btn_map = (LinearLayout)
	 * getActivity().findViewById(R.id.ll_btn_map); ll_btn_tieba =
	 * (LinearLayout) getActivity().findViewById(R.id.ll_btn_tieba); ll_info
	 * = (LinearLayout) getActivity().findViewById(R.id.ll_info);
	 * 
	 * ib_btn_info = (ImageButton)
	 * getActivity().findViewById(R.id.ib_btn_info); ib_btn_about =
	 * (ImageButton) getActivity().findViewById(R.id.ib_btn_about);
	 * ib_btn_logout = (ImageButton)
	 * getActivity().findViewById(R.id.ib_btn_logout);
	 * 
	 * // more_notice_dialog = (TextView) //
	 * getActivity().findViewById(R.id.more_notice_dialog);
	 * 
	 * OnClickListener clickListener = new OnButtonClickListener();
	 * 
	 * ll_btn_cet.setOnClickListener(clickListener);
	 * ll_btn_map.setOnClickListener(clickListener);
	 * ll_btn_xuanke.setOnClickListener(clickListener);
	 * ll_btn_tieba.setOnClickListener(clickListener);
	 * ll_info.setOnClickListener(clickListener);
	 * ib_btn_info.setOnClickListener(clickListener);
	 * ib_btn_about.setOnClickListener(clickListener);
	 * ib_btn_logout.setOnClickListener(clickListener);
	 */

        // more_notice_dialog.setOnClickListener(clickListener);

        ScrollView scrollView = (ScrollView) getActivity().findViewById(R.id.block);
        mCoolDragAndDropGridView = (CoolDragAndDropGridView) getActivity().findViewById(R.id.coolDragAndDropGridView);

        mItems.add(new Item(R.drawable.mclass, 3, "学生选课", "选修/删除公选课，查看选课信息。"));
        mItems.add(new Item(R.drawable.cet, 3, "四六级报名", "暂时只支持打开四六级报名网页"));
        mItems.add(new Item(R.drawable.pj, 3, "评教", "暂时只支持打开评教网页"));
        mItems.add(new Item(R.drawable.msgnote, 3, "留言板", "学习、生活、吐槽"));
        // mItems.add(new Item(R.drawable.cet, 3, "CET", "功能没做好你放上去有毛线用！"));
        // mItems.add(new Item(R.drawable.share, 3, "推广",
        // "学校周边创业的亲maybe感兴趣^_^"));
        mItems.add(new Item(R.drawable.info_normal, 3, "建议", "发现BUG、新的想法和建议"));
        mItems.add(new Item(R.drawable.handshake, 3, "App源码", "爱学习的小伙伴来看看吧"));
        mItems.add(new Item(R.drawable.about_normal, 3, "关于App", "大家多多支持"));

        mItemAdapter = new ItemAdapter(getActivity(), mItems);
        mCoolDragAndDropGridView.setAdapter(mItemAdapter);
        mCoolDragAndDropGridView.setScrollingStrategy(new SimpleScrollingStrategy(scrollView));
        mCoolDragAndDropGridView.setDragAndDropListener(this);
        mCoolDragAndDropGridView.setOnItemLongClickListener(this);
        mCoolDragAndDropGridView.setOnItemClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (MainActivity.student != null) {
                // 转到选课界面
                Intent intent = new Intent();
                intent.setClass(getActivity(), XkActivity.class);
                startActivity(intent);
            }
        } else if (requestCode == 1) {
            if (MainActivity.student != null) {
                Intent notIntent = new Intent();
                notIntent.setClass(getActivity(), NoteActivity.class);
                startActivity(notIntent);
            }
        } else if(requestCode == CET_REQUEST){
            if(MainActivity.student!=null){
                Intent itt = new Intent();
                itt.putExtra("title", "正在加载网页...");
                itt.putExtra("url", G.SILIUJI);
                itt.setClass(getActivity(), NewsActivity.class);
                startActivity(itt);
            }
        } else if(requestCode == PJ_REQUEST){
            if(MainActivity.student!=null){
                Intent itt = new Intent();
                itt.putExtra("title", "正在加载网页...");
                itt.putExtra("url", G.PJ);
                itt.setClass(getActivity(), NewsActivity.class);
                startActivity(itt);
            }
        }
    }

    @Override
    public void onClick(View v) {

    }

    public static final int CET_REQUEST = 2;
    public static final int PJ_REQUEST = 3;
    public void onBlockClick(int iconId) {
        Intent itt;
        switch (iconId) {
            case R.drawable.pj:
                if (MainActivity.student == null) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivityForResult(intent, PJ_REQUEST);
                } else {
                    itt = new Intent();
                    itt.putExtra("title", "正在加载网页...");
                    itt.putExtra("url", G.PJ);
                    itt.setClass(getActivity(), NewsActivity.class);
                    startActivity(itt);
                }
                break;
            case R.drawable.cet:
                if (MainActivity.student == null) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivityForResult(intent, CET_REQUEST);
                } else {
                    itt = new Intent();
                    itt.putExtra("title", "正在加载网页...");
                    itt.putExtra("url", G.SILIUJI);
                    itt.setClass(getActivity(), NewsActivity.class);
                    startActivity(itt);
                }
                break;
            case R.drawable.mclass:
                if (MainActivity.student == null) {
                    // 转到分数查询,如果没有登录,先登录.
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivityForResult(intent, 0);
                } else {
                    // 转到选课界面
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), XkActivity.class);
                    startActivity(intent);
                }
                break;

            case R.drawable.msgnote:
                if (MainActivity.student == null) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivityForResult(intent, 1);
                } else {
                    Intent notIntent = new Intent();
                    notIntent.setClass(getActivity(), NoteActivity.class);
                    startActivity(notIntent);
                }
                break;
            case R.drawable.share:

                break;
            case R.drawable.info_normal:
                Intent fbitt = new Intent();
                fbitt.setClass(getActivity(), FeedbackActivity.class);
                startActivity(fbitt);
                break;
            case R.drawable.about_normal:
                itt = new Intent();
                itt.setClass(getActivity(), AboutActivity.class);
                startActivity(itt);
                break;
            case R.drawable.handshake:
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://git.oschina.net/edhlily/Yangtzeu_jwc");
                intent.setData(content_url);
                startActivity(intent);
                break;
            case R.drawable.logout_normal:
                if (MainActivity.student == null) {
                    Toast.makeText(getActivity(), "还没有登录", Toast.LENGTH_SHORT).show();
                } else {
                    if (ConfirmDialog.show(getActivity(), "注销", "注销将清除相关数据及保存的密码，确定吗？")) {
                        MainActivity.student = null;
                        StudentServiceImpl.getInstance().clearStudent(getActivity());
                        Toast.makeText(getActivity(), "注销成功", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        mCoolDragAndDropGridView.startDragAndDrop();

        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Item item = (Item) arg0.getItemAtPosition(arg2);
        if (item != null) {
            onBlockClick(item.getIcon());
        }
    }

    @Override
    public void onDragItem(int from) {

    }

    @Override
    public void onDraggingItem(int from, int to) {

    }

    @Override
    public void onDropItem(int from, int to) {
        if (from != to) {
            mItems.add(to, mItems.remove(from));
            mItemAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public boolean isDragAndDropEnabled(int position) {
        return false;
    }
}
